import { _decorator, Component, Label, Node } from 'cc';
import { GroundGenerator } from './GroudGenerator';
import { PlayerController } from './PlayerController';
const { ccclass, property } = _decorator;

enum GameState {
    GAME_MENU,
    GAMME_PLAYING
}
@ccclass('GameManager')
export class GameManager extends Component {
    private _gameState: GameState = null;

    @property(GroundGenerator)
    public groundGenerator: GroundGenerator = null;

    @property(PlayerController)
    public playerController: PlayerController = null;

    @property(Node)
    public startMenu: Node = null;

    @property(Label)
    public stepLabel: Label = null;

    gameStart() {
        this.startMenu.active = false;
        this._gameState = GameState.GAMME_PLAYING;
        this.groundGenerator.generateGround();
        this.playerController.node.on("jumpStepEvent", this.onJumpStepEvent, this);
        this.playerController.onGameStart();
    }

    gameOver() {
        this.startMenu.active = true;
        this.playerController.onGameEnd();
        this._gameState = GameState.GAME_MENU;
    }

    onJumpStepEvent(step: number) {
        this.stepLabel.string = step.toString();
        if (this.groundGenerator.isGameOver(step)) {
            this.gameOver();
        }
    }
}

